package com.fenghongzhang.day016;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private EditText edit;
    private Button btn;
    private ListView lv;
    private static final String TAG = "Main2Activity";
    private List<Music> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        edit = (EditText) findViewById(R.id.edit);
        btn = (Button) findViewById(R.id.btn);
        lv = (ListView) findViewById(R.id.lv);

        //创建数据源
        /**
         * 异步下载
         * musicbean
         * xiaomusicbean
         */
        //适配器.


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = edit.getText().toString();
                new MyMusicBean().execute("http://musicapi.leanapp.cn/search/suggest?keywords="+"dengziqi");
            }
        });

        list = new ArrayList<>();



    }

    class MyMusicBean extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... strings) {
            StringBuffer sb=new StringBuffer();
            HttpURLConnection connection=null;
            InputStream inputStream=null;
            try {
                URL url=new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode()==200){
                    inputStream  = connection.getInputStream();
                    byte[] b=new byte[1024];
                    int len=0;
                    while((len=inputStream.read(b))!=-1){
                        sb.append(new String(b,0,len));
                    }
                    return sb.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    connection.disconnect();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i(TAG, "onPostExecute: "+s);
            MusicBean musicBean = new Gson().fromJson(s, MusicBean.class);
            List<MusicBean.ResultBean.SongsBean> songs = musicBean.getResult().getSongs();
            for (int i = 0; i < songs.size(); i++) {
                MusicBean.ResultBean.SongsBean songsBean = songs.get(i);
                int id = songsBean.getId();
                Log.i(TAG, "onPostExecute: "+id);
                new MyMusicBean2().execute("http://musicapi.leanapp.cn/music/url?id="+id);
                //name
                Music music = new Music();
                music.setTitle(songsBean.getName());
                music.setId(id);
                list.add(music);
            }
        }
    }

    class MyMusicBean2 extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... strings) {
            StringBuffer sb=new StringBuffer();
            HttpURLConnection connection=null;
            InputStream inputStream=null;
            try {
                URL url=new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode()==200){
                    inputStream  = connection.getInputStream();
                    byte[] b=new byte[1024];
                    int len=0;
                    while((len=inputStream.read(b))!=-1){
                        sb.append(new String(b,0,len));
                    }
                    return sb.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    connection.disconnect();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i(TAG, "onPostExecute: "+s);
            XiaoMusicBean xiaoMusicBean = new Gson().fromJson(s, XiaoMusicBean.class);
            List<XiaoMusicBean.DataBean> data = xiaoMusicBean.getData();
            for (int i = 0; i < data.size(); i++) {
                XiaoMusicBean.DataBean dataBean = data.get(i);
                String url = dataBean.getUrl();
                //url
                Log.i(TAG, "onPostExecute: "+url);
//                if(dataBean.getId() == )
                for (int j = 0; j < list.size(); j++) {
                    Music music = list.get(j);
                    if(music.getId() ==  dataBean.getId()){
                        music.setData(url);
//                        list.set(j,music);
                    }
                }
            }
            MyAdapter myAdapter = new MyAdapter(list,Main2Activity.this);
            lv.setAdapter(myAdapter);

//
//            for (int j = 0; j < list.size(); j++) {
//                Music music = list.get(j);
//                Log.i(TAG, "onPostExecute: "+music);
//            }

        }
    }


}
